package com.zxs.Util;

import org.apache.commons.codec.binary.Base64;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

public class PaymentUtil {
    public static String appendParam(String returns, String paramId, String paramValue) {
        if (returns != "") {
            if (paramValue != "") {
                returns += "&" + paramId + "=" + paramValue;
            }
        } else {
            if (paramValue != "") {
                returns = paramId + "=" + paramValue;
            }
        }
        return returns;
    }


    //生成签名
    public static String signMsg( String signMsg) {

        String base64 = "";
        try {

            KeyStore ks = KeyStore.getInstance("PKCS12");
            //加载密钥库，商家的私钥证书
            String file = PaymentUtil.class.getResource("sandbox.privateKey.pfx").getPath().replaceAll("%20", " ");
            System.out.println(file);

            FileInputStream ksfis = new FileInputStream(file);

            BufferedInputStream ksbufin = new BufferedInputStream(ksfis);

            //私钥证书的访问密码
            char[] keyPwd = "123456".toCharArray();
            //char[] keyPwd = "YaoJiaNiLOVE999Year".toCharArray();
            ks.load(ksbufin, keyPwd);
            //私钥证书的访问别名
            PrivateKey priK = (PrivateKey) ks.getKey("20240619.3300000010258522", keyPwd);
            Signature signature = Signature.getInstance("SHA256withRSA");
            signature.initSign(priK);
            signature.update(signMsg.getBytes("utf-8"));
            /*sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
            base64 = encoder.encode(signature.sign());*/
            Base64 encoder = new Base64();
            base64 = encoder.encodeAsString(signature.sign());

        } catch(FileNotFoundException e){
            System.out.println("123");
        }catch (Exception ex) {
            ex.printStackTrace();
        }
        System.out.println("test = "+base64);
        return base64;
    }

    /**
     * 验证签名
     * @param val 拼接的字符串
     * @param msg 快钱发送的签名值
     * @return
     */
    public static boolean enCodeByCer( String val, String msg) {
        boolean flag = false;
        try {
            //证书文件，快钱公钥
            String file = PaymentUtil.class.getResource("sandbox.publicKey.cer").toURI().getPath();//99bill[1].cert.rsa.20140803.cer
            System.out.println(file);                       //  99bill.cert.rsa.20140803.cer
            FileInputStream inStream = new FileInputStream(file);

            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            X509Certificate cert = (X509Certificate) cf.generateCertificate(inStream);

            PublicKey pk = cert.getPublicKey();

            Signature signature = Signature.getInstance("SHA256withRSA");
            signature.initVerify(pk);
            signature.update(val.getBytes());

            Base64 decoder = new Base64();
            decoder.decode(msg);
            flag = signature.verify(decoder.decode(msg));

            System.out.println(flag);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("no");
        }
        return flag;
    }

}
